(function () {

Array.prototype.indexOf = function (val) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == val) return i;
    }
    return -1;
};

Array.prototype.remove = function (val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};

 window.onload = function() {
        game.init();
    }
     var game = window.game = {
     	stage:null,//舞台
     	ticker:null,//定时器
     	asset: null,//图片组
     	bg :null,//背景
        bg1:null,
     	state: null,
     	width: 0,
        height: 0,
        myPlane:null,//飞机
        enemyPlanes:null,//敌人飞机
        bullets:null,//子弹数组
        ifbullet:false,//是否发射子弹
        audio:null,
        enemyI:0,//敌机id编号
        score:0,//计分
        currentScore:null,//当前分数显示
        enemyPlanes1:null,//敌机1
        enemyI1:0,//敌机1编号
     	init: function () {
     		this.asset = new game.Asset();
            this.asset.on('complete', function(e){
            this.asset.off('complete');
            this.initStage();

        }.bind(this));
            this.asset.load();
        },
     	initStage: function () {
            var asd =[{x:110, y:110}, {x:120, y:120}];
     		this.width = Math.min(innerWidth, 450) * 2;
            this.height = Math.min(innerHeight, 750) * 2;
            this.scale = innerWidth/720;
            //创建舞台
           this.stage = new Hilo.Stage({
            	renderType:'canvas',
                boundsArea:asd,
            	width: this.width,
            	height: this.height,
            	scaleX: this.scale,
                scaleY: this.scale
            });
            document.body.appendChild(this.stage.canvas);
            this.enemyPlanes=[];
            this.enemyI=0;
            //创建定时器
            this.ticker = new Hilo.Ticker(60);
            this.ticker.addTick(this.stage);
            this.ticker.addTick(Hilo.Tween);
            this.ticker.start();
          // //绑定交互事件
          //   this.stage.enableDOMEvent(Hilo.event.POINTER_START, true);
          //   this.stage.on(Hilo.event.POINTER_START, this.onUserInput.bind(this));
            this.initBackground();
            this.initCurrentScore();
            this.initmyPlane();
            this.initLogical();
            this.openThread();

     	},
        initCurrentScore:function () {
            //当前分数
            this.currentScore = new Hilo.BitmapText({
                id: 'score',
                glyphs: this.asset.numberGlyphs,
                scaleX:0.5,
                scaleY:0.5,
                textAlign: 'center'
            }).addTo(this.stage);

            //设置当前分数的位置
           this.currentScore.setText('0');
           this.currentScore.x = (this.currentScore.width>>2)+10;
           this.currentScore.y = 10;
           this.currentScore.textAlign='right';
        },
     	initBackground: function() {
            //背景
            var bgWidth = this.width * this.scale;
            var bgHeight = this.height * this.scale;
            var bgImg = this.asset.bgfj;

            this.bg = new Hilo.Bitmap({
                id: 'bg',
                x:0,
                y:0,
                image: bgImg,
                scaleX: this.width / bgImg.width,
                scaleY: this.height / bgImg.height
            }).addTo(this.stage);

            this.bg1 = new Hilo.Bitmap({
                id: 'bg1',
                x:0,
                y:-bgImg.height*this.height / bgImg.height+2,
                image: bgImg,
                scaleX: this.width / bgImg.width,
                scaleY: this.height / bgImg.height
            }).addTo(this.stage);

            console.log(":"+"-"+bgImg.height*this.height / bgImg.height);
            Hilo.Tween.to(this.bg, {
                x:0,
                y:bgImg.height*this.height / bgImg.height,
                alpha:1
            },{
                duration:10000,
                delay:10,
                loop: true,
                onComplete:function(){
                  //  console.log('complete');
                }
            });
            Hilo.Tween.to(this.bg1,{
                x:0,
                y:0,
                alpha:1
            },{
                duration:10000,
                delay:10,
                loop: true,
                onComplete:function(){
                    //console.log('complete');
                }
            });
             Hilo.WebSound.getAudio({
                src: './sounds/menu.mp3',
                loop: true,
                volume: 0.8
            }).on('load', function(e){
               // console.log('load');
            }).on('end', function(e){
              //  console.log('end');
            }).play();
         //   console.log(this.audio);

        },
        initmyPlane:function () {
            this.myPlane =new game.myPlane({
                id:'myPlane',
                x:10,
                y:10,
                currentFrame:0,
                loop:true,
            }).addTo(this.stage,3);
            this.myPlane.addFrame(this.asset.myPlane.getSprite('myPlane'));
            //var myPlaneImg =this.asset.myPlane

        },
        initenemyPlanes:function () {
            if (this.enemyPlanes==null) {
                 this.enemyPlanes=[];
            }

            let x = Math.ceil(Math.random() * this.stage.width-150);
            x=x>150?(x<this.stage.width-150?x:this.stage.width-150):10;
            let y = Math.ceil(Math.random() * this.stage.height/3);
            y=y>150?(y<this.stage.height/3-150?y:this.stage.height/3-150):10;
            let enemyPlane =new game.enemyPlane({
                id:'enemyPlane'+this.enemyI,
                x:x,
                y:-150,
                interval:1.5,
                currentFrame:0,
                loop:true,
            }).addTo(this.stage);
            enemyPlane.addFrame(this.asset.enemyPlane.getSprite('enemyPlane'));
            this.enemyPlanes.push(enemyPlane);
            this.enemyI++;
        },
        initenemyPlanes1:function () {
            if (this.enemyPlanes1==null) {
                 this.enemyPlanes1=[];
            }

            let x = Math.ceil(Math.random() * this.stage.width-150);
            x=x>150?(x<this.stage.width-150?x:this.stage.width-150):10;
            let y = Math.ceil(Math.random() * this.stage.height/3);
            y=y>150?(y<this.stage.height/3-150?y:this.stage.height/3-150):10;
            let enemyPlane1 =new game.enemyPlane1({
                id:'enemyPlane1'+this.enemyI1,
                x:x,
                y:-150,
                interval:1.5,
                currentFrame:0,
                scaleX: 1.8,
                scaleY: 1.8,
                loop:true,
            }).addTo(this.stage);
            enemyPlane1.addFrame(this.asset.enemyPlane1.getSprite('enemyPlane1'));
            this.enemyPlanes1.push(enemyPlane1);
            this.enemyI1++;
        },
        openThread:function () {
            let that =this;
             if (typeof(Worker)!=="undefined") {
               if (typeof(enemyPlane)=="undefined") {
                //生产敌机
                enemyPlane =new Worker("js/production.js");
            }
              enemyPlane.onmessage = function (e) {
                if (e.data==1) {
                     that.initenemyPlanes();
                    //生产敌机
                }else if (e.data==2) {
                     that.initEnemyPlanesLogical();
                    //敌机移动速度
                }else if (e.data==3) {
                    if (that.ifbullet) {
                        //生产子弹
                    that.initbullets();
                    }
                }else if (e.data==4) {
                    //子弹移动速度
                    that.bulletsLogical();
                }else if (e.data==11) {
                    that.initenemyPlanes1();
                }else if (e.data==21) {
                     that.initEnemyPlanesLogical1();
                }
              }
          }else{
                console.log("抱歉，你的浏览器不支持 Web Workers...");
            }
        },
       initEnemyPlanesLogical:function () {
           for (var i =0; i<this.enemyPlanes.length; i++) {
            if (this.enemyPlanes[i].y>this.stage.height) {
                //从画布中删除已经飞出屏幕的敌机
                this.enemyPlanes[i].removeFromParent(this.stage);
                  //数组中删除已经飞出屏幕的敌机
                this.enemyPlanes.splice(i,1);
                console.log('移除屏幕'+this.enemyPlanes[i].id);
             //   console.log('帧数++'+ this.enemyPlanes[i]+":"+ this.enemyPlanes[i].getNumFrames());
              //  console.log(this.enemyPlanes[i]);
                //console.log("敌机数："+this.enemyPlanes.length);
            }else{
            Hilo.Tween.to(this.enemyPlanes[i], {
                x:this.enemyPlanes[i].x,
                y:this.enemyPlanes[i].y+1,
            },{
                duration:1,
            });
        }
    }
       },
       initEnemyPlanesLogical1:function () {
           for (var i =0; i<this.enemyPlanes1.length; i++) {
            if (this.enemyPlanes1[i].y>this.stage.height) {
                //从画布中删除已经飞出屏幕的敌机
                this.enemyPlanes1[i].removeFromParent(this.stage);
                  //数组中删除已经飞出屏幕的敌机
                this.enemyPlanes1.splice(i,1);
                console.log('移除屏幕'+this.enemyPlanes1[i].id);
             //   console.log('帧数++'+ this.enemyPlanes[i]+":"+ this.enemyPlanes[i].getNumFrames());
              //  console.log(this.enemyPlanes[i]);
                //console.log("敌机数："+this.enemyPlanes.length);
            }else{
            Hilo.Tween.to(this.enemyPlanes1[i], {
                x:this.enemyPlanes1[i].x,
                y:this.enemyPlanes1[i].y+1,
            },{
                duration:1,
            });
        }
    }
       },
       initLogical:function () {
        this.state =false;
             let start = Hilo.event.POINTER_START,
                move = Hilo.event.POINTER_MOVE,
                end = Hilo.event.POINTER_END;
            let that =this;
            //开启交互
            this.stage.enableDOMEvent([start, move, end], true);
            this.stage.on(start, function (e) {
               //  console.log("nian"+that.myPlane)
               // console.log(e.stageX);
               if (e.stageX>that.myPlane.x&&e.stageX<that.myPlane.x+that.myPlane.width&&e.stageY>that.myPlane.y&&e.stageY<that.myPlane.y+that.myPlane.height) {
                that.state =true;
                 //开启子弹发射
                that.ifbullet =true;
               }
               // that.state =true
                  //调用飞机逻辑
                // that.planeLogical(that.state,e);
            });
            this.stage.on(move, function (e) {
               // console.log("nian"+that.myPlane)
                 //调用飞机逻辑
               that.planeLogical(that.state,e);
            });
            this.stage.on(end, function (e) {
                that.state =false;
                //  //调用飞机逻辑
                // that.planeLogical(that.state,e);
                //关闭子弹发射
                that.ifbullet =false;
                //that.state =
            });
        },
        planeLogical:function (state,e) {
           // console.log(state);
            if (state) {
                let x =e.stageX-this.myPlane.width/2>0?(e.stageX-this.myPlane.width/2<this.stage.width-this.myPlane.width-10?e.stageX-this.myPlane.width/2:this.stage.width-this.myPlane.width-10):0;
                    let y =e.stageY-this.myPlane.height/2>0?(e.stageY-this.myPlane.height/2<this.stage.height-this.myPlane.height-10?e.stageY-this.myPlane.height/2:this.stage.height-this.myPlane.height-10):0;
                  //  console.log("sds"+this.stage.height+","+y);
                    Hilo.Tween.to(this.myPlane,{
                        x:x,
                        y:y,
                    },{
                        duration:2,
                    });
            }
        },
        bulletsLogical:function () {
            if (this.bullets==null) {
                this.bullets=[];
            }
           // console.log("子弹数:"+this.bullets.length);
            for (var i =0; i <this.bullets.length; i++) {
                if (this.bullets[i].y<-20) {
                    this.bullets[i].removeFromParent(this.stage);
                    this.bullets.splice(i,1);
                }else{
                    Hilo.Tween.to(this.bullets[i], {
                        x:this.bullets[i].x,
                        y:this.bullets[i].y-6,
                    },{
                        duration:1,
                    });
                }
            }
        },
        initbullets:function () {
            if (this.bullets==null) {
                    this.bullets=[];
                }
         Hilo.WebSound.getAudio({
                src: './sounds/shot.ogg',
                loop: false,
                volume: 1
            }).on('load', function(e){
             //   console.log('load');
            }).on('end', function(e){
                //console.log('end');
            }).play();
                let that =this;
                let bullet =new game.bullet({
                    id:'bullet'+this.bullets.length,
                    image:this.asset.bullet,
                    x:this.myPlane.x+this.myPlane.width/2-10,
                    y:this.myPlane.y-25,
                    onUpdate:function () {
                        let zidanState=true;
                        //let than =this;
                        // let enemyPlanesABC=that.enemyPlanes.slice();
                        if (zidanState) {
                        for(var j=0;j<that.enemyPlanes.length;j++){
                           // if(this.x+this.width>that.enemyPlanes[j].x&&this.x<that.enemyPlanes[j].x+that.enemyPlanes[j].width&&this.y<that.enemyPlanes[j].y+that.enemyPlanes[j].height&&this.y+this.height>that.enemyPlanes[j].y){
                                let enemyPlane =that.enemyPlanes[j];
                                if (this.hitTestObject(enemyPlane,false)) {
                                    enemyPlane.being--;
                                    console.log('生命'+enemyPlane.being);
                                    if (enemyPlane.being<=0) {
                                    Hilo.WebSound.getAudio({
                                        src: './sounds/bomb.ogg',
                                        loop: false,
                                        volume: 1
                                    }).play();
                                    enemyPlane.stop();
                                    enemyPlane.addFrame(that.asset.explosions.getSprite('explosions'));
                                    console.log('打中'+enemyPlane.id+":"+enemyPlane.getNumFrames());
                                    this.removeFromParent(that.stage);
                                    that.bullets.remove(this);
                                    enemyPlane.loop=false;
                                    enemyPlane.interval=4;
                                    that.enemyPlanes.remove(enemyPlane);
                                    that.score+=10;
                                    that.currentScore.setText(that.score.toString());
                                    that.currentScore.x = (that.currentScore.width>>1)+10;
                                    that.currentScore.y = 10;
                                    that.currentScore.textAlign='right';
                                    enemyPlane.setFrameCallback(11,function () {
                                        this.removeFromParent(that.stage);
                                        console.log(this);
                                        console.log('打中且删出'+enemyPlane.id);
                                    });

                                    enemyPlane.play();

                              //  }
                                  break;
                              }else{
                                 this.removeFromParent(that.stage);
                                 that.bullets.remove(this);
                              }
                              zidanState =false;
                            }
                        }
                    }
                         if (zidanState) {
                        for(var j=0;j<that.enemyPlanes1.length;j++){
                             let enemyPlane1 =that.enemyPlanes1[j];
                                if (this.hitTestObject(enemyPlane1,false)) {
                                    enemyPlane1.being--;
                                    if (enemyPlane1.being<=0) {
                                    Hilo.WebSound.getAudio({
                                        src: './sounds/bomb.ogg',
                                        loop: false,
                                        volume: 1
                                    }).play();
                                    enemyPlane1.stop();
                                    enemyPlane1.addFrame(that.asset.enemyPlane_baozha1.getSprite('enemyPlane_baozha1'));
                                    console.log('打中'+enemyPlane1.id+":"+enemyPlane1.getNumFrames());
                                    this.removeFromParent(that.stage);
                                    that.bullets.remove(this);
                                    enemyPlane1.loop=false;
                                    enemyPlane1.interval=4;
                                    that.enemyPlanes1.remove(enemyPlane1);
                                    that.score+=5;
                                    that.currentScore.setText(that.score.toString());
                                    that.currentScore.x = (that.currentScore.width>>1)+10;
                                    that.currentScore.y = 10;
                                    that.currentScore.textAlign='right';
                                    enemyPlane1.setFrameCallback(5,function () {
                                        this.removeFromParent(that.stage);
                                        console.log(this);
                                        console.log('打中且删出'+enemyPlane1.id);
                                    });

                                    enemyPlane1.play();

                              //  }
                                  break;
                              }else{
                                 this.removeFromParent(that.stage);
                                 that.bullets.remove(this);
                              }
                              zidanState =false;
                            }
                        }
                    }
                    }
                }).addTo(this.stage);
                this.bullets.push(bullet);
               // console.log('子弹'+this.bullets);
        },
        explosionsDestruction:function (e) {
            //爆炸销毁敌人飞机
        }

     }
	//舞台画布
  // var renderType = location.search.indexOf('dom') != -1 ? 'dom' : 'canvas';

// sprite.on(Hilo.event.POINTER_START, function(e){
//     console.log(e.eventTarget, e.stageX, e.stageY);
// });

// 接下来，您就可以开始利用hilo提供的各种可视类来创建各种图形

})();